package com.cms.loader;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cms.common.StringUtils;

import freemarker.cache.FileTemplateLoader;

public class CmsFileTemplateLoader extends FileTemplateLoader {
	
	private Logger logger = LoggerFactory.getLogger(CmsFileTemplateLoader.class);

	public CmsFileTemplateLoader() throws IOException {
		super();
	}
	
    public long getLastModified(final Object templateSource) {
        return System.currentTimeMillis();
    }

	public Object findTemplateSource(final String name) throws IOException {
		return StringUtils.replace(name, "_zh_CN", "");
	}

	public Reader getReader(Object templateSource, String encoding) throws IOException {
		InputStream fis = CmsFileTemplateLoader.class.getClassLoader().getResourceAsStream((String) templateSource);
		try
		{
			return new InputStreamReader(fis, encoding);
		}catch(Exception ex)
		{
			logger.error("getReader.error:{},msg:{}",templateSource,ex.getMessage());
			return null;
		}
		
	}
}
